<?php

namespace app\admin\controller\live;

use app\common\controller\AdminController;
use app\common\service\live\LiveService;
use app\common\service\store\CommentService;
use app\Request;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\View;

/**
 * @ControllerAnnotation(title="live")
 */
class Live extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\live\Live();
        
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $data =LiveService::liveInfo($page,$limit,$where);
            return json($data);
        }
        return $this->fetch();
    }


    /**
     * Author:YangXinPeng
     * 获取播流推流地址
     * @param Request $request
     */
    public function buildLive(Request $request)
    {
        $id = $request->get('id');
        $data = LiveService::byId($id);
        $streamName= $data['live_no'];
        $time = $data['live_can_days'];
        $arr=[];
        $pushUrl = LiveService::getPushUrl($streamName,$time);
        $pullUrl = LiveService::getPullUrl($streamName,$time);
        $arr['pull']=$pullUrl;
        $arr['push'] = $pushUrl;
        LiveService::saveUrl($pullUrl,$pushUrl,$id);
        return $this->success('成功',$arr);
    }

    /**
     * Author:YangXinPeng
     * 下拉主播
     * @return \think\response\Json
     */
    public function hosts()
    {
        $data = LiveService::hosts();
        return json($data);
    }

    /**
     * Author:YangXinPeng
     * 去直播按钮
     * @param Request $request
     * @return string
     */
    public function live(Request $request)
    {
        $id = $request->get('id');
        $data = LiveService::byId($id);
        View::assign('data',$data);
        return View::fetch();
    }


    public function getName(Request $request)
    {
        $id = $request->get('id');
        $data = LiveService::nameById($id);
        return json($data);
    }

    
}